package com.tmanager.persistence.entity;

import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Entity
@Table(name = "t_activity")
@NamedQueries({ @NamedQuery(name = Activity.GET_BY_USER_AND_DATES, query = "select a from Activity a where a.userId = :userId and a.subActivity is not NULL"),
		 })
public class Activity extends IdentifiedEntity{
	public static final String GET_BY_USER_AND_DATES="getByUserAndDates";
	private static final Logger log = LoggerFactory.getLogger(Activity.class);
	@Column(length = 1024,nullable = false)
	private String name;
	private int type;
	private long timestamp;
	@OneToOne
    @JoinColumn(name = "subActId", nullable = true)
	private Activity subActivity;
	private long userId;
	private long activityId;
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setType(ActivityType type) {
		this.type = type.getId();
	}
	public ActivityType getType() {
		return ActivityType.convert(type);
	}
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}
	public long getTimestamp() {
		return timestamp;
	}
	public void setSubActivity(Activity subActivity) {
		this.subActivity = subActivity;
	}
	public Activity getSubActivity() {
		return subActivity;
	}
	public void setUserId(long userId) {
		this.userId = userId;
	}
	public long getUserId() {
		return userId;
	}
	public void setActivityId(long activityId) {
		this.activityId = activityId;
	}
	public long getActivityId() {
		return activityId;
	}
	
	public static final String JSON_ACTIVITY_ID = "id";
	public static final String JSON_NAME = "name";
	public static final String JSON_TYPE = "type";
	public static final String JSON_TIMESTAMP = "time";
	public static final String JSON_SUB_ACTIVITY = "sub";
	
	public JSONObject toJSON() throws JSONException {
		JSONObject obj = new JSONObject();
		obj.put(JSON_ACTIVITY_ID, activityId);
		obj.put(JSON_NAME, name);
		if (subActivity!= null){
			obj.put(JSON_SUB_ACTIVITY, subActivity.toJSON());
		}
		return obj;
	}
}
